#!/usr/bin/env python
# coding=utf-8

"""
@author: yaqiwe
@contact: 15338230927@163.com
@file: main.py
@date: 2020/10/28 23:13
@desc: 爬取文件的入口类
"""

import _thread
import time

from service.AmazonSearch import AmazonSearch
from util.LogginUtil import logging


class Main:

    def start(self, i, maxi):
        i += 1
        a = AmazonSearch()
        CurrentlyUnavailableList = []
        while i < maxi:
            logging.info('当前查找搜索结果页数：{}'.format(i))
            res = a.searchInfo('socks', i)
            urllist = a.getShopUrlList(res.text.encode('utf8'))
            list = a.isCurrentlyUnavailable(urllist)
            if len(list) > 0:
                CurrentlyUnavailableList.extend(list)
                timestr = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
                logging.info('当前时间：{}  获取到僵尸链接数：{}'.format(timestr, len(CurrentlyUnavailableList)))
                filePath = './CurrentlyUnavailableList.json'
                with open(filePath, 'w', encoding='utf-8') as f:
                    logging.info('结果写入文件：{}'.format(filePath))
                    f.write('\n'.join(CurrentlyUnavailableList))
            else:
                logging.info('当前页未找到僵尸listing')

    def startThread(self):
        i = 0

        _thread.start_new_thread(self.start,(i, 10000))
        _thread.start_new_thread(self.start,(i, 10000))
        _thread.start_new_thread(self.start,(i, 10000))
        _thread.start_new_thread(self.start,(i, 10000))
        _thread.start_new_thread(self.start,(i, 10000))


if __name__ == '__main__':
    # Main().startThread()
    a = AmazonSearch()
    CurrentlyUnavailableList = []
    for i in range(1,1000):
        logging.info('当前查找搜索结果页数：{}'.format(i))
        res = a.searchInfo('sock', i)
        urllist = a.getShopUrlList(res.text.encode('utf8'))
        list = a.isCurrentlyUnavailable(urllist)
        if len(list) > 0:
            CurrentlyUnavailableList.extend(list)
            timestr = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
            logging.info('当前时间：{}  获取到僵尸链接数：{}'.format(timestr, len(CurrentlyUnavailableList)))
            filePath = './CurrentlyUnavailableList.json'
            with open(filePath, 'w', encoding='utf-8') as f:
                logging.info('结果写入文件：{}'.format(filePath))
                f.write('\n'.join(CurrentlyUnavailableList))
        else:
            logging.info('当前页未找到僵尸listing')